import Taro from '@tarojs/taro';
import { AtButton } from 'taro-ui';
import { View, Navigator, ScrollView } from '@tarojs/components'
import { useEffect } from 'react';

import './index.less';

import { useProfile } from '../../state/profile';


function Apply() {
  const { profile, profileLoading, fetchProfile } = useProfile()

  useEffect(() => {
    if (profile?.code !== undefined && profile?.code !== 0) {
      wx.hideHomeButton()
      Taro.showToast({
        title: profile?.message || '未知错误',
        icon: 'none',
        duration: 2000
      })
    } else if (profile?.code === 0) {
      Taro.switchTab({
        url: '/pages/home/index'
      })
    }
  }, [profile])

  return (
    <ScrollView
      scrollY
      scrollWithAnimation
      refresherEnabled
      refresherTriggered={profileLoading}
      onRefresherRefresh={fetchProfile}
      refresherThreshold={45}
      style={{height: '100vh'}}
    >
      <View className='header'>
        <View className='header-name'>
          用户无权限
        </View>
      </View>
      <View className='body'>
        {
          profile?.code === 10005 &&
          <View>已申请，请等待管理员通过</View>
        }
        {
          profile?.code === 10006 &&
          (
            <View className='apply-register'>
              <View>还未注册，请注册</View>
              <Navigator url='/pages/register/index'>
                <AtButton type='primary' className='apply-register__button'>立即注册</AtButton>
              </Navigator>
            </View>
          )
        }
        {
          profile?.code === 10007 &&
          <View>用户申请已拒绝，请联系管理员</View>
        }
      </View>
    </ScrollView>
  );
}

export default Apply;
